package com.jw.server.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.ydx.protobuf.OTIpcDef;
import cn.ydx.protobuf.OTIpcDef.OTIpcList;

import com.jw.server.model.PositionDriver;
import com.jw.server.model.PositionVehicle;
import com.jw.server.service.PositionDriverRepository;
import com.jw.server.service.PositionVehicleRepository;
import com.jw.server.util.ByteUtil;

/**
 * 定位服务数据解析入库
 * 
 * @author xfm 2018年5月17日
 */
@RestController
@RequestMapping("/position")
public class PositionServiceDataController {

	@Autowired
	PositionVehicleRepository positionVehicleRepository;

	@Autowired
	PositionDriverRepository positionDriverRepository;

	@PostMapping("/driver")
	public String positionDriver(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<PositionDriver> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.PositionDriver> li = o.getPositionDriverList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.PositionDriver> l = li.iterator();
					basepay = new ArrayList<PositionDriver>();
					while (l.hasNext()) {
						OTIpcDef.PositionDriver base = l.next();
						basepay.add(PositionDriver.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收positionDriver表的数据条数："+basepay.size());
			positionDriverRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

	@PostMapping("/vehicle")
	public String positionVehicle(@RequestBody byte[] content) {
		byte[] b = null;
		// 解压数据
		b = ByteUtil.decompress(content);
		try {
			// 拿到第一层
			OTIpcList b1 = OTIpcDef.OTIpcList.parseFrom(b);
			ListIterator<OTIpcDef.OTIpc> iter = b1.getOtpicList()
					.listIterator();
			List<PositionVehicle> basepay = null;
			while (iter.hasNext()) {
				OTIpcDef.OTIpc o = iter.next();
				List<OTIpcDef.PositionVehicle> li = o.getPositionVehicleList();
				if (li != null && li.size() > 0) {
					Iterator<OTIpcDef.PositionVehicle> l = li.iterator();
					basepay = new ArrayList<PositionVehicle>();
					while (l.hasNext()) {
						OTIpcDef.PositionVehicle base = l.next();
						basepay.add(PositionVehicle.convertPojo(base));
					}
				}
			}
			// 将数据存入数据库 basepay
			System.out.println("收positionVehicle表的数据条数："+basepay.size());
			positionVehicleRepository.save(basepay);

		} catch (Exception e) {
			e.printStackTrace();
			return "400";
		}
		return "200";
	}

}
